package com.lenovo.manufacture.module;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;

import com.lenovo.manufacture.annotation.Layout;
import com.lenovo.manufacture.net.Results;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Module16 extends BaseActivity {
    List<Map<String, String>> userPlStepInfos;
    RvAdapter adapter;
    @BindView(R.id.rv_module16)
    RecyclerView rvModule16;
    String lineStepId;

    @Layout(value = R.layout.activity_module16, title = "查询生产线环节信息")
    protected void init() {
        progressDialog.show();
        userPlStepInfos = new ArrayList<>();
        adapter = new RvAdapter();
        rvModule16.setLayoutManager(new GridLayoutManager(this, 5));
        rvModule16.setAdapter(adapter);
        //查询第四车间生产线Id
        async(apiService.userLineSearch(param.add("pos", 3).build()), data -> {
            //查询学生生产环节
            Map<String, String> userLine = data.getData().get(0);
            String lineId = userLine.get("lineId");
            lineStepId = userLine.get("lineStepId");
            async(apiService.searchUserPlStep(param.add("lineId", lineId).build()), this::getUserPlStepInfo);
        });
    }

    private void getUserPlStepInfo(Results data1) {
        new Thread(() -> {
            for (Map<String, String> userPlStep : data1.getData()) {
                try {
                    Map<String, String> userPlStepInfo = apiService.getUserPlStepInfo(param.add("id", userPlStep.get("id")).build()).execute().body().getData().get(0);
                    userPlStepInfos.add(userPlStepInfo);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            rvModule16.post(() -> {
                adapter.notifyDataSetChanged();
                progressDialog.cancel();
            });
        }).start();
    }

    class RvAdapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_module16, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Map<String, String> userPlStepInfo = userPlStepInfos.get(position);
            holder.tv1.setText(userPlStepInfo.get("plStepName"));
            holder.tv2.setText("HP:" + userPlStepInfo.get("power") + "/100");
            if (lineStepId.equals(userPlStepInfo.get("id"))) {
                holder.ll1.setBackgroundColor(Color.parseColor("#82AAFF"));
            }
            if (Integer.parseInt(userPlStepInfo.get("power")) < Integer.parseInt(userPlStepInfo.get("consume"))) {
                holder.iv1.setImageResource(R.drawable.warn);
                holder.iv1.setBackgroundColor(Color.parseColor("#aa000000"));
            }
        }

        @Override
        public int getItemCount() {
            return userPlStepInfos.size();
        }
    }

    static
    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_1)
        TextView tv1;
        @BindView(R.id.ll_1)
        LinearLayout ll1;
        @BindView(R.id.iv_1)
        ImageView iv1;
        @BindView(R.id.tv_2)
        TextView tv2;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}